<template>
	<view style="height: 100%;">
		<view class="head">
			<view class="item" :class="itemActive==0?'active':'ZW'" @click="itemBtn(0)">待付款</view>
			<view class="item" :class="itemActive==1?'active':'ZW'" @click="itemBtn(1)">待服务</view>
			<view class="item" :class="itemActive==2?'active':'ZW'" @click="itemBtn(2)">已完成</view>
			<view class="item" :class="itemActive==4?'active':'ZW'" @click="itemBtn(4)">退款</view>
		</view>
		<view class="content">
			<!-- 待付款 -->
			<view class="item" v-if="itemActive==0?true:false">
				<view class="order_hint" v-if="ifShow">
					<image src="../../static/image/order_hint.png"></image>
					<view>您还没有相关的订单</view>
				</view>
				<view class="it" v-for="(item, index) in orderInfo" :key="index">
					<view class="it-hd">
						<view>住家月子服务({{item.days}}天)</view>
						<view>全额: ￥{{item.payable_price}}元</view>
					</view>
					<view class="it-bd">
						<view class="headImg">
							<image :src="item.head_url" mode=""></image>
						</view>
						<view class="desc">
							<view class="userName">{{item.name}}<view><image src="/static/image/attestation1.png"></image>母婴学院认证</view></view>
							<view class="priceBox">待支付：<text class="price">￥{{item.payable_price}}元</text></view>
							<view class="time">预约时间：{{item.start_time}}</view>
						</view>
						<view class="non-payment">未支付</view>
					</view>
					<view class="it-ft">
						<button class="off" @click="offBtn(item.id,index)">取消订单</button>
						<button class="submit" @click="getOrderBtn(item.id)">立即付款</button>
					</view>
					<view class="odd">订单编号: {{item.ordersn}}</view>
				</view>
			</view>
			<!-- 待服务 -->
			<view class="item" v-if="itemActive==1?true:false">
				<view class="order_hint" v-if="ifShow">
					<image src="../../static/image/order_hint.png"></image>
					<view>您还没有相关的订单</view>
				</view>
				<view class="it" v-for="(item, index) in orderInfo" :key="index">
					<view class="it-hd">
						<view>住家月子服务({{item.days}}天)</view>
						<view>全额: ￥{{item.payable_price}}元</view>
					</view>
					<view class="it-bd">
						<view class="headImg">
							<image :src="item.head_url" mode=""></image>
						</view>
						<view class="desc">
							<view class="userName">{{item.name}}<view><image src="/static/image/attestation1.png"></image>母婴学院认证</view></view>
							<view class="priceBox">已支付：<text class="price">￥{{item.payable_price}}元</text></view>
							<view class="time">预约时间：{{item.start_time}}</view>
						</view>
						<view class="non-payment">已支付</view>
					</view>
					<view class="it-ft">
						<button class="submit" @click="refundBtn(item.id)">点击退款</button>
					</view>
					<view class="odd">订单编号: {{item.ordersn}}</view>
				</view>
			</view>
			<!-- 已付款 -->
			<view class="item" v-if="itemActive==2?true:false">
				<view class="order_hint" v-if="ifShow">
					<image src="../../static/image/order_hint.png"></image>
					<view>您还没有相关的订单</view>
				</view>
				<view class="it" v-for="(item, index) in orderInfo" :key="index">
					<view class="it-hd">
						<view>住家月子服务({{item.days}}天)</view>
						<view>全额: ￥{{item.payable_price}}元</view>
					</view>
					<view class="it-bd">
						<view class="headImg">
							<image  :src="item.head_url" mode=""></image>
						</view>
						<view class="desc">
							<view class="userName">{{item.name}}<view><image src="/static/image/attestation1.png"></image>母婴学院认证</view></view>
							<view class="priceBox">已支付：<text class="price">￥{{item.payable_price}}元</text></view>
							<view class="time">预约时间：{{item.start_time}}</view>
						</view>
						<!-- <view class="non-payment">已支付</view> -->
					</view>
					<view class="it-ft">
						<button class="submit" :class="item.is_evaluate == 0 ? 'ZW' : 'active'" @click="openEvaluate(item.id)" :disabled="item.is_evaluate == 0 ? false : true">{{item.is_evaluate == 0 ? '评价' : '已评价'}}</button>
					</view>
					<view class="odd">订单编号: {{item.ordersn}}</view>
				</view>
			</view>
			<!-- 已退款 -->
			<view class="item" v-if="itemActive==4?true:false">
				<view class="order_hint" v-if="ifShow">
					<image src="../../static/image/order_hint.png"></image>
					<view>您还没有相关的订单</view>
				</view>
				<view class="it" v-for="(item, index) in orderInfo" :key="index">
					<view class="it-hd">
						<view>住家月子服务({{item.days}}天)</view>
						<view>全额: ￥{{item.payable_price}}元</view>
					</view>
					<view class="it-bd">
						<view class="headImg">
							<image  :src="item.head_url" mode=""></image>
						</view>
						<view class="desc">
							<view class="userName">{{item.name}}<view><image src="/static/image/attestation1.png"></image>母婴学院认证</view></view>
							<view class="priceBox">已支付：<text class="price">￥{{item.payable_price}}元</text></view>
							<view class="time">预约时间：{{item.start_time}}</view>
						</view>
						<view class="non-payment">已支付</view>
					</view>
					<view class="it-ft">
						<button class="submit" style="border: 1px solid #d0d0d0;color: #7f7f7f;">{{item.status == 4 ? '退款中' : '已退款'}}</button>
					</view>
					<view class="odd">订单编号: {{item.ordersn}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { dns } from '@/pages/dns.js'
	export default{
		data () {
			return {
				itemActive: 0,
				orderInfo: [],
				ifShow: false,
				ifdelete: 0//为1時删除元素
			}
		},
		components:{
			
		},
		watch:{
			ifShow(newVal,oldVal){
				// console.log(newVal,oldVal)
			}
		},
		onLoad(e) {
			var itemA = uni.getStorageSync("itemActive");
			if( itemA!==0 && itemA!==1){
				this.getorderInfo(0);
			}
		},
		onShow(e) {
			var itemA = uni.getStorageSync("itemActive");
			if( itemA==0 || itemA==1){
				this.itemActive = itemA;
				this.getorderInfo(itemA);
				uni.setStorageSync("itemActive",99)
			}
			//评价完成刷新页面
			let pages = getCurrentPages();
			let currPage = pages[pages.length-1];//当前页面
			console.log(currPage.ifdelete)
			if(currPage.ifdelete){
				this.getorderInfo(2);
			}
		},
		methods: {
			itemBtn(num){
				this.itemActive = num;
				this.getorderInfo(num);
			},
			getorderInfo(status){//获取代付款订单
				uni.request({
					method: 'GET',
					data: {
						status
					},
					header:{
						token: uni.getStorageSync("token")
					},
					url: dns + 'order/list',
					success: (res)=>{
						console.log(res);
						this.orderInfo = [];
						if(res.data.status == -1){
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
							this.ifShow = true;
							return;
						}
						if(res.data.status == 1){
							this.ifShow = false;
							this.orderInfo = res.data.data;
						}
						if(res.data.status == 0){
							this.ifShow = true;
						}
					}
				})
			},
			offBtn(id,ind){//取消订单
			let that = this;
				uni.showModal({
				    title: '提示',
					confirmColor: '#f4984b',
				    content: '您确定要取消订单吗?',
				    success: function (res) {
				        if (res.confirm) {
				            // console.log('用户点击确定');
							uni.request({
								method: 'POST',
								data: {
									id
								},
								header: {
									token: uni.getStorageSync("token")
								},
								url: dns + 'order/cancel',
								success: (res)=>{
									console.log(res)
									if(res.data.status == 1){
										uni.showToast({
											title: res.data.msg
										})
										that.orderInfo.splice(ind,1);
										if(that.orderInfo.length == 0){
											that.ifShow = true
										}
									}
								}
							})
				        } else if (res.cancel) {
				            // console.log('用户点击取消');
				        }
				    }
				});
			},
			getOrderBtn(order_id){//立即支付
				uni.request({
					method:'GET',
					data: {
						id: order_id
					},
					header:{
						token: uni.getStorageSync("token")
					},
					url: dns + 'pay/getdata',
					success: (res)=>{
						// console.log(res)
						if(res.data.status == 1){
							var that = this;
							uni.requestPayment({
							    provider: 'wxpay',
							    timeStamp: res.data.data.timeStamp+'',
							    nonceStr: res.data.data.nonceStr,
							    package: res.data.data.package,
							    signType: 'MD5',
							    paySign: res.data.data.paySign,
							    success: function (res) {
							        uni.showToast({
							        	title:'支付成功!'
							        })
									that.itemActive = 1;
									that.getorderInfo(1);
							    },
							    fail: function (err) {
									uni.showToast({
										icon: 'none',
										title:'支付失败!'
									})
							    }
							});
						}
					}
				})
			},
			refundBtn(id){//退款
				uni.showModal({
				    title: '提示',
				    content: '您确定要退款吗?',
					confirmColor: '#f4984b',
				    success: (res)=>{
				        if (res.confirm) {
				            // console.log('用户点击确定');
							uni.request({
								url: dns + 'order/refund',
								method: 'GET',
								data: {
									id
								},
								header:{
									token: uni.getStorageSync("token")
								},
								success: (res)=>{
									console.log(res)
									if(res.data.status == 1){
										uni.showToast({
											title:res.data.msg
										})
										this.itemActive = 4;
										this.getorderInfo(4);
									}
								}
							});
				        } else if (res.cancel) {
				            // console.log('用户点击取消');
				        }
				    }
				});
			},
			openEvaluate(id){//进入评价页面
				uni.navigateTo({
					url: './evaluate/index?id='+id
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	.head{
		display: flex;
		width: 100%;
		background-color: #f6f6f6;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
		.item{
			width: 25%;
			line-height: 76upx;
			text-align: center;
			font-size: 28upx;
		}
		.item.active{
			color: #ff5c5d;
		}
		.item.active::after{
			content: '';
			display: block;
			height: 4upx;
			width: 40upx;
			margin: 0 auto;
			margin-top: -4upx;
			background-color: #ff5c5d;
		}
	}
	.content{
		background-color: #f6f6f6;
		height: 100%;
		.item{
			position: relative;
			background-color: #f6f6f6;
			padding-top: 76upx;
			height: 100%;
			.order_hint{
				position: absolute;
				top: 40%;
				left: 50%;
				transform: translate(-50%,-50%);
				image{
					width: 400upx;
					height: 400upx;
				}
				view{
					text-align: center;
					font-size: 30upx;
					margin-top: -50upx;
				}
			}
			.it{
				position: relative;
				padding: 20upx 40upx;
				background-color: #fff;
				margin-bottom: 14upx;
				.it-hd{
					font-size: 28upx;
					color: #000;
					display: flex;
					justify-content: space-between;
				}
				.it-bd{
					display: flex;
					margin-top: 30upx;
					position: relative;
					.headImg{
						width: 110upx;
						height: 110upx;
						border-radius: 50%;
						overflow: hidden;
						image{
							width: 100%;
							height: 100%;
						}
					}
					.desc{
						margin-left: 30upx;
						.userName{
							display: flex;
							font-size: 34upx;
							margin-bottom: 6upx;
							view{
								position: relative;
								padding-left: 48upx;
								font-size: 20upx;
								color: #f5994c;
								line-height: 46upx;
								image{
									position: absolute;
									top: 6upx;
									left: 0;
									width: 32upx;
									height: 32upx;
									line-height: 36upx;
									margin-left: 14upx;
								}
							}
						}
						.priceBox{
							font-size: 24upx;
							color: #7f7f7f;
							margin-bottom: 6upx;
							.price{
								color: #f5994c;
							}
						}
						.time{
							font-size: 24upx;
							color: #7f7f7f;
						}
					}
					.non-payment{
						position: absolute;
						right: 0;
						top: 0;
						color: #f5984b;
						font-size: 22upx;
					}
				}
				.it-ft{
					margin-top: 30upx;
					text-align: right;
					height: 50upx;
					button{
						margin: 0;
						border: none;
						width: 156upx;
						padding: 0;
						display: inline-block;
						background-color: #fff;
						font-size: 24upx;
						line-height: 52upx;
						text-align: center;
						border: 2upx solid #d0d0d0;
						border-radius: 30upx;
						margin-left: 20upx;
						color: #7f7f7f;
					}
					.submit{
						border: 2upx solid #f3a47c;
						color: #f5984b;
					}
					.active{
						border: 2upx solid #dddddd;
						color: #7f7f7f;
					}
					button::after{
						content: '';
						width: 0;
						height: 0;
					}
				}
				.odd{
					position: absolute;
					bottom: 20upx;
					left: 40upx;
					font-size: 24upx;
					color: rgba(0,0,0,.3);
				}
			}
		}
	}
</style>
